﻿Public Class SharedItemMediaInfoControl
    Public Event SetImdbLink(ByVal link As Uri)

    Private mPosterSizeAtLoad As Drawing.Size
    Private mPosterLocationAtLoad As Drawing.Point
    Private mPosterSizeAtNewImage As Drawing.Size
    Private mPosterLocationAtNewImage As Drawing.Point
    Private mMediaInfo As Entities.MediaInfo
    Private mOpenMediaInfo As Entities.OpenMediaInfo.OpenMediaInfo
    Private mFeature As Entities.OpenMediaInfo.FeatureInfo

    Public Sub UpdateView(ByVal item As Entities.SharedItem, ByVal files As Generic.List(Of Entities.SharedItemFile), ByVal mediaInfo As Entities.MediaInfo)
        mPosterSizeAtLoad = picPoster.Size
        mPosterLocationAtLoad = picPoster.Location
        ClearDisplay()
        UpdateSharedItem(item)
        UpdateMediaInfo(mediaInfo)
    End Sub

    Private Sub UpdateSharedItem(ByVal item As Entities.SharedItem)
        If item IsNot Nothing Then
            lblTitle.Text = item.Name
        End If
    End Sub

    Private Sub ClearDisplay()
        lblTitle.Text = "Loading..."
        picPoster.Image = Nothing
        pnlLogos.Controls.Clear()
        mNextLogoX = 2
    End Sub

    Private Sub UpdateMediaInfo(ByVal mediaInfo As Entities.MediaInfo)
        If mediaInfo Is Nothing Then Return

        mMediaInfo = mediaInfo
        If mediaInfo IsNot Nothing Then mOpenMediaInfo = mediaInfo.OpenMediaInfo
        If mOpenMediaInfo IsNot Nothing Then mFeature = mOpenMediaInfo.Feature

        lblTitle.Text = String.Format("{0} ({1})", GetFeatureTitle, GetFeatureYear)
        UpdateOpenMediaInfo()
    End Sub
    Private Sub UpdateOpenMediaInfo()
        If mFeature IsNot Nothing Then
            If mFeature.Ids IsNot Nothing Then
                For Each strId As String In mFeature.Ids
                    If strId IsNot Nothing AndAlso strId.StartsWith("imdb:") Then
                        strId = strId.Substring(5)
                        Dim uri As Uri = OpenMediaInfo.IMDB.Manager.GetUriFromId(strId)
                        RaiseEvent SetImdbLink(uri)
                    End If
                Next
            End If
            txtPlotSummary.Text = mFeature.PlotSummary
            txtDirectors.Text = GetDirectors(", ", ", and ")
            txtWriters.Text = GetWriters(", ", ", and ")
            txtCast.Text = GetCast(10, ", ", ", and ")
            If mFeature.Poster IsNot Nothing Then
                Using clsIO As New System.IO.MemoryStream(mFeature.Poster.Data)
                    Dim clsImage As New Drawing.Bitmap(clsIO)
                    picPoster.Image = clsImage

                    Dim factX As Double = picPoster.Image.Width / mPosterSizeAtLoad.Width
                    Dim factY As Double = picPoster.Image.Height / mPosterSizeAtLoad.Height
                    Dim fact As Double = CDbl(IIf(factX > factY, factX, factY))

                    Dim iNewW As Integer = CInt(picPoster.Image.Width / fact)
                    Dim iNewH As Integer = CInt(picPoster.Image.Height / fact)

                    Dim iNewX As Integer = CInt(mPosterLocationAtLoad.X + (mPosterSizeAtLoad.Width - iNewW) / 2)
                    Dim iNewY As Integer = CInt(mPosterLocationAtLoad.Y + (mPosterSizeAtLoad.Height - iNewH) / 2)

                    mPosterLocationAtNewImage = New Drawing.Point(iNewX, iNewY)
                    mPosterSizeAtNewImage = New Drawing.Size(iNewW, iNewH)

                    picPoster.SetBounds(iNewX, iNewY, iNewW, iNewH)
                End Using
            End If
        End If
        lblTitle.Text = String.Format("{0} ({1})", GetFeatureTitle, GetFeatureYear)
        If mOpenMediaInfo IsNot Nothing AndAlso mOpenMediaInfo.Streams IsNot Nothing Then
            For Each clsSI As Entities.OpenMediaInfo.StreamInfo In mOpenMediaInfo.Streams
                Dim iId As Integer = 0
                For Each clsVS As Entities.OpenMediaInfo.VideoStreamInfo In clsSI.VideoStreams
                    iId += 1
                    If iId = 1 Then
                        txtPicture.Text = clsVS.Format & " " & clsVS.FormatProfile & " " & _
                                        clsVS.Width & "x" & clsVS.Height & " " & _
                                        clsVS.Framerate & " " & clsVS.Bitrate
                    End If
                    Select Case clsVS.Width
                        Case Is >= 1880 '=1920-40
                            AddIcon("FullHD", My.Resources.Video_FullHD, "Full HD Quality Video")
                        Case Is >= 1240 '=1280-30
                            AddIcon("HD", My.Resources.Video_HD, "HD Quality Video")
                        Case Is >= 716 '736-20
                            AddIcon("DVD", My.Resources.Video_DVD, "DVD Quality Video")
                    End Select
                    If clsVS.Format = "AVC" Then
                        AddIcon("AVC", My.Resources.H264, "H.264/AVC Video Encoding")
                    End If
                Next

                Dim clsSBAudio As New System.Text.StringBuilder
                For Each clsAI As Entities.OpenMediaInfo.AudioStreamInfo In clsSI.AudioStreams
                    If clsSBAudio.Length > 0 Then clsSBAudio.Append(", ")
                    clsSBAudio.Append(clsAI.Language & ": " & clsAI.Format & " " & clsAI.FormatProfile & " " & _
                                          clsAI.ChannelPositions & " " & clsAI.Bitrate)
                    If clsAI.Format = "DTS" Then AddIcon("DTS", My.Resources.DTS, "DTS Audio")
                    If clsAI.Format = "AC3" Then AddIcon("AC3", My.Resources.DolbyDigital, "Dolby Digital Audio")
                    If clsAI.Format = "AAC" Then AddIcon("AAC", My.Resources.AAC, "Advanced Audio Codec Audio")
                Next
            Next
        End If

    End Sub

    Private mNextLogoX As Integer = 2
    Private mIconKeys As New Generic.Dictionary(Of String, Boolean)
    Private Sub AddIcon(ByVal key As String, ByVal pic As Drawing.Icon, ByVal toolTip As String)
        If mIconKeys.ContainsKey(key) Then Return
        mIconKeys.Add(key, True)
        Dim clsPicBox As New PictureBox
        clsPicBox.SizeMode = PictureBoxSizeMode.StretchImage
        clsPicBox.Image = pic.ToBitmap
        clsPicBox.Size = pic.Size
        clsPicBox.Location = New Drawing.Point(mNextLogoX, 2)
        pnlLogos.Controls.Add(clsPicBox)
        ToolTip1.SetToolTip(clsPicBox, toolTip)
        mNextLogoX = clsPicBox.Location.X + clsPicBox.Size.Width + 6
    End Sub
    Private Sub AddIcon(ByVal key As String, ByVal pic As Drawing.Bitmap, ByVal toolTip As String)
        If mIconKeys.ContainsKey(key) Then Return
        mIconKeys.Add(key, True)
        Dim clsPicBox As New PictureBox
        clsPicBox.Image = pic
        clsPicBox.Size = pic.Size
        clsPicBox.Location = New Drawing.Point(mNextLogoX, 2)
        pnlLogos.Controls.Add(clsPicBox)
        ToolTip1.SetToolTip(clsPicBox, toolTip)
        mNextLogoX = clsPicBox.Location.X + clsPicBox.Size.Width + 6
    End Sub

    Private mResizePercent As Integer
    Private mResizeStep As Integer

    Private Sub picPoster_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles picPoster.MouseEnter
        If picPoster.Image IsNot Nothing Then
            mResizeStep = 25
            tmrResizePoster.Enabled = True
        End If
    End Sub

    Private Sub picPoster_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles picPoster.MouseLeave
        If picPoster.Image IsNot Nothing Then
            mResizeStep = -40
            tmrResizePoster.Enabled = True
        End If
    End Sub

    Private Sub tmrResizePoster_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles tmrResizePoster.Tick
        mResizePercent += mResizeStep
        If mResizeStep < 0 Then
            'shrinking
            If mResizePercent <= 0 Then
                tmrResizePoster.Enabled = False
                mResizePercent = 0
            End If
        ElseIf mResizeStep > 0 Then
            If mResizePercent >= 200 Then
                tmrResizePoster.Enabled = False
                mResizePercent = 200
            End If
        End If
        Dim iNewWidth As Integer = CInt((picPoster.Image.Width - mPosterSizeAtNewImage.Width) * mResizePercent / 100 + mPosterSizeAtNewImage.Width)
        Dim iNewHeight As Integer = CInt((picPoster.Image.Height - mPosterSizeAtNewImage.Height) * mResizePercent / 100 + mPosterSizeAtNewImage.Height)
        picPoster.Size = New Drawing.Size(iNewWidth, iNewHeight)
    End Sub

    Private Function GetFeatureYear() As String
        If mFeature IsNot Nothing Then Return mFeature.Year
        Return "?"
    End Function
    Private Function GetFeatureTitle() As String
        If mFeature IsNot Nothing Then Return mFeature.Title
        Return "?"
    End Function
    Private Function GetDirectors(ByVal separator As String, ByVal lastSeparator As String) As String
        If mFeature IsNot Nothing AndAlso mFeature.Directors IsNot Nothing Then
            Return GetContributers(mFeature.Directors.ToArray, separator, lastSeparator)
        Else
            Return "?"
        End If
    End Function
    Private Function GetWriters(ByVal separator As String, ByVal lastSeparator As String) As String
        If mFeature IsNot Nothing AndAlso mFeature.Directors IsNot Nothing Then
            Return GetContributers(mFeature.Writers.ToArray, separator, lastSeparator)
        Else
            Return "?"
        End If
    End Function
    Private Function GetCast(ByVal maxCount As Integer, ByVal separator As String, ByVal lastSeparator As String) As String
        If mFeature IsNot Nothing AndAlso mFeature.Directors IsNot Nothing Then
            If maxCount > mFeature.Cast.Count Then
                Return GetContributers(mFeature.Cast.Take(maxCount).ToArray, separator, lastSeparator)
            Else
                Return GetContributers(mFeature.Cast.ToArray, separator, lastSeparator)
            End If
        Else
            Return "?"
        End If
    End Function
    Private Function GetContributers(ByVal list() As Entities.OpenMediaInfo.Contributer, ByVal separator As String, ByVal lastSeparator As String) As String
        If list Is Nothing Then Return "?"
        Dim clsSB As New System.Text.StringBuilder
        For i As Integer = 0 To list.Count - 1
            If i = 0 Then
            ElseIf i = list.Count - 1 Then
                clsSB.Append(lastSeparator)
            Else
                clsSB.Append(separator)
            End If
            clsSB.Append(list(i).Name)
        Next
        Return clsSB.ToString
    End Function

    Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.Click

    End Sub
End Class
